package com.netease.qa.biz.service.impl;

import com.jayway.jsonpath.JsonPath;
import com.netease.qa.biz.service.EngineService;
import com.netease.qa.biz.utils.JsonUtils;
import io.restassured.response.Response;
import io.restassured.response.ResponseBody;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.restassured.RestAssured.*;

/**
 * @author zoudongdong
 * @date 2018/3/9
 */
@Service
public class EngineServiceImpl implements EngineService {

    public Map<String, String> doGetMethod(String url,
                                           Map<String, Object> headers,
                                           Map<String, Object> params) {
        Map<String, String> result = new HashMap<String, String>();

        Response response;
        if (headers != null && params != null) {
            response = given().
                                headers(headers).
                                params(params).
                        when().
                                get(url);
        } else if (headers != null) {
            response = given().
                                headers(headers).
                        when().
                                get(url);
        } else if (params != null) {
            response = given().
                                params(params).
                        when().
                                get(url);
        } else {
            response = get(url);
        }
        ResponseBody responseBody = response.getBody();
        result.put("responseBody", responseBody.print());
        return result;
    }

    public Map<String, String> doGetMethodAndValidateJsonPath(String url,
                                                              Map<String, Object> headers,
                                                              Map<String, Object> params,
                                                              String jsonPathCheckPoint) {
        Map result = doGetMethod(url, headers, params);
        return validateJsonPath(result, jsonPathCheckPoint);
    }

    public Map<String, String> doPostMethod(String url,
                                            Map<String, Object> headers,
                                            String bodyJson) {
        Map<String, String> result = new HashMap<String, String>();

        Response response;
        if (headers != null && bodyJson != null) {
            response = given().
                                headers(headers).
                                body(bodyJson).
                        when().
                                post(url);
        } else if (headers != null) {
            response = given().
                                headers(headers).
                        when().
                                post(url);

        } else if (bodyJson != null) {
            response = given().
                                body(bodyJson).
                        when().
                                post(url);
        } else {
            response = post(url);
        }
        ResponseBody responseBody = response.getBody();
        result.put("responseBody", responseBody.print());
        return result;
    }

    public Map<String, String> doPostMethodAndValidateJsonPath(String url,
                                                               Map<String, Object> headers,
                                                               String bodyJson,
                                                               String jsonPathCheckPoint) {
        Map<String, String> result = doPostMethod(url, headers, bodyJson);
        return validateJsonPath(result, jsonPathCheckPoint);
    }

    private Map<String, String> validateJsonPath(Map<String, String> result, String jsonPathCheckPoint) {
        String responseBody = result.get("responseBody");

        //对返回结果进行校验
        if (JsonUtils.isGoodJson(responseBody)) {
            //get请求返回的结果是json格式的
            String[] checkPointAttr = jsonPathCheckPoint.split("&&");
            StringBuilder sb = new StringBuilder();
            for (String checkPoint : checkPointAttr) {//$.code!=200
                sb.append("................." + checkPoint);

                String regx = "([^==|!=|>=|>|<=|<]*)(==|!=|>=|>|<=|<)([^==|!=|>=|>|<=|<]*)";
                Pattern pattern = Pattern.compile(regx);
                Matcher matcher = pattern.matcher(checkPoint);
                if (matcher.find()) {
                    String exp = matcher.group(1);//$.code
                    String judge = matcher.group(2);//>=
                    String compareValue = matcher.group(3);//200
                    String actualValue = JsonPath.parse(responseBody).read(exp, String.class);

                    if(judge.equals("==")){
                        if (actualValue.equals(compareValue)) {
                            sb.append("  校验通过<br/>");
                        } else {
                            sb.append("  校验不通过<br/>");
                        }
                    }if(judge.equals("!=")){
                        if (!actualValue.equals(compareValue)) {
                            sb.append("  校验通过<br/>");
                        } else {
                            sb.append("  校验不通过<br/>");
                        }
                    } else if(judge.equals(">=")){
                        if (Integer.parseInt(actualValue) >= Integer.parseInt(compareValue)) {
                            sb.append("  校验通过<br/>");
                        } else {
                            sb.append("  校验不通过<br/>");
                        }
                    } else if(judge.equals(">")){
                        if (Integer.parseInt(actualValue) > Integer.parseInt(compareValue)) {
                            sb.append("  校验通过<br/>");
                        } else {
                            sb.append("  校验不通过<br/>");
                        }
                    }else if(judge.equals("<=")){
                        if (Integer.parseInt(actualValue) <= Integer.parseInt(compareValue)) {
                            sb.append("  校验通过<br/>");
                        } else {
                            sb.append("  校验不通过<br/>");
                        }
                    }else if(judge.equals(">=")){
                        if (Integer.parseInt(actualValue) < Integer.parseInt(compareValue)) {
                            sb.append("  校验通过<br/>");
                        } else {
                            sb.append("  校验不通过<br/>");
                        }
                    }
                }
            }
            result.put("checkPointResult", sb.toString());
        }
        return result;
    }
}
